import bean.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class LeetCode872 {
    List<TreeNode> list1 = new ArrayList<>();
    List<TreeNode> list2 = new ArrayList<>();
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        dp(list1, root1);
        dp(list2, root2);

        if (list1.size() != list2.size()){
            return false;
        }

        for (int i = 0; i < list1.size(); i++){
            if (list1.get(i).val != list2.get(i).val){
                return false;
            }
        }
        return true;
    }

    public void dp(List<TreeNode> list, TreeNode node){
        if (node.left != null){
            dp(list, node.left);
        }

        if (node.right != null){
            dp(list, node.right);
        }

        if (node.left == null && node.right == null){
            list.add(node);
        }
    }
}
